<?php
/**
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

jimport('joomla.application.component.modellist');

/**
 * Methods supporting a list of banner records.
 *
 * @package		BaiToai.Nguyen
 * @subpackage	com_exercises
 * @since		1.6
 */
class ExercisesModelResults extends JModelList
{
	/**
	 * Constructor.
	 *
	 * @param	array	An optional associative array of configuration settings.
	 * @see		JController
	 * @since	1.6
	 */
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'id', 'a.id',
                                'userid', 'a.userid','exerid'
			);
		}

		parent::__construct($config);
	}

	/**
	 * Method to auto-populate the model state.
	 *
	 * Note. Calling getState in this method will result in recursion.
	 *
	 * @since	1.6
	 */
	protected function populateState($ordering = null, $direction = null)
	{
		// Initialise variables.
		$app = JFactory::getApplication('administrator');

		// Load the filter state.
		$params = JComponentHelper::getParams('com_exercises');

		// List state information.
		parent::populateState('a.id', 'asc');
	}
	protected function getStoreId($id = '')
	{
		// Compile the store id.

		return parent::getStoreId($id);
	}

	/**
	 * Build an SQL query to load the list data.
	 *
	 * @return	JDatabaseQuery
	 */
	protected function getListQuery()
	{
		// Create a new query object.
		$db = $this->getDbo();
		$query = $db->getQuery(true);

		// Select the required fields from the table.
		$query->select(
			$this->getState(
				'list.select',
				'a.id AS id,'.
                                'a.userid AS userid,'.
				'a.exerid AS exerid,'.
				'a.starttime AS starttime, ' .
				'a.stoptime AS stoptime,'.
				'a.totalpoint AS totalpoint,'.
				'u.name AS uname,'.
				'e.name AS ename,'.
				'a.status AS status'
			)
		);

		$query->from($db->quoteName('#__exercises_results').' AS a');
                $query->leftJoin($db->quoteName('#__users'). ' AS u ON a.userid = u.id');
                $query->leftJoin($db->quoteName('#__exercises'). ' AS e ON a.exerid = e.id');
		$orderCol	= $this->state->get('list.ordering', 'ordering');
		$orderDirn	= $this->state->get('list.direction', 'ASC');
		$query->order($db->escape($orderCol.' '.$orderDirn));

		//echo nl2br(str_replace('#__','jos_',$query));
              
		return $query;
	}
}
